var Experience=document.getElementById("Experience"),
    change1_2=document.getElementById("change1_2"),
    leftImg=document.getElementById("left"),
    rightImg=document.getElementById("right"),
    footerImg=document.getElementsByClassName("footerImg"),
    footerP=document.getElementsByClassName("footerP"),
    play=document.getElementsByClassName("play"),
    oFlash1=document.getElementById("flash_1"),
    circle=document.getElementsByClassName("circle");
var temp= 1,time1,time2,timerReplay;
/////初始化
Experience.getElementsByTagName("img")[0].style.left=-225+'px';
for(var i=0;i<play.length;i++){////////轮播图
    //play[i].style.left=i*1349+"px";

    if(i==0) play[i].style.left=0+"px";
    else  play[i].style.left=1349+"px";
}
/////事件绑定
for(var i=0;i<footerImg.length;i++){//////页脚图
    footerImg[i].addEventListener("mouseover",create,false);
    footerImg[i].addEventListener("mouseout",dead,false);
}
for(var i=0;i<footerP.length;i++){//////页脚文字描述
    footerP[i].addEventListener("mouseover",stopPropagation,false);
    footerP[i].addEventListener("mouseout",stopPropagation,false);
}
for(var i=0;i<circle.length;i++){//////轮播切换按钮
    circle[i].addEventListener("click",changePhoto,false);
}
document.addEventListener("scroll",scroll,false);

//函数区
function judge(){
    if(temp==1){
        time1 = new Date();
        temp=2;
    }else{
        time2 = new Date();
        temp=1;
    }
    if(time2==undefined||Math.abs(time2-time1)>1000) {
        clearInterval(timerReplay);
        move();
    }
    else return;
}
//////轮播
timerReplay = setInterval(move,3500);
//////切换按钮
function changePhoto(){
    if(temp==1){
        time1 = new Date();
        temp=2;
    }else{
        time2 = new Date();
        temp=1;
    }
    if(time2==undefined||Math.abs(time2-time1)>1000) {
        clearInterval(timerReplay);
        for(var i=0;i<circle.length;i++){
            if(this.id==circle[i].id){
                move(i);
            }
        }
    } else return;
}
var current=0;
//////轮播图片移动
function move (k) {
    if(k==undefined){
        for(var i=0;i<play.length;i++){
            if(play[i].style.left==0+"px"){
                play[i].style.left=-1349+"px";
                play[i].style.transition="left .8s";
            }else if(i==current+1){
                play[i].style.left=0+"px";
                play[i].style.transition="left .8s";
            }else{
                play[i].style.left=1349+"px";
            }
            if(play[i].style.left<0+"px"){
                hiddenPhoto();
            }
            if(play[i].style.left==0+"px"){
                circle[i].style.background="black";
            }else{
                circle[i].style.background="#a4a5a6";
            }
        }
        current++;
        if(current==play.length-1) current=-1;
    }else{
        for(var i=0;i<play.length;i++){
            if(i==k){
                play[i].style.display="block";
                play[i].style.left=0+"px";
                play[i].style.transition="left .8s";
            }else if(play[i].style.left==0+"px"){
                play[i].style.left=-1349+"px";
                play[i].style.transition="left .8s";
            }
            if(play[i].style.left<0+"px"){
                hiddenPhoto();
            }
            if(play[i].style.left==0+"px"){
                circle[i].style.background="black";
            }else{
                circle[i].style.background="#a4a5a6";
            }
        }
    }
}
//////隐藏和显示left小于零的图片
function hiddenPhoto(){
    setTimeout(function () {
        for(var i=0;i<play.length;i++){
            if(play[i].style.left<0+"px"){
                play[i].style.display="none";
                play[i].style.left=1349+"px";
                setTimeout(function () {
                    for(var i=0;i<play.length;i++){
                        if(play[i].style.left==1349+"px"){
                            play[i].style.display="block";
                        }
                    }
                },100)
            }
        }
    },800)
}
//////页脚图片动效
function create(){
    var footerP=this.getElementsByClassName("footerP");
    for(var i=0;i<footerP.length;i++){
        footerP[i].style.top="-200px";
        footerP[i].style.visibility="visible";
        footerP[i].style.transition="top 1s,visibility 1s 0.5s";
        footerP[footerP.length-1].style.top="-145px";
    }
    var spanT=document.createElement("span"),
        spanB=document.createElement("span"),
        spanL=document.createElement("span"),
        spanR=document.createElement("span");
    createSpan(this,"10%",null,"10%",null,spanT,"span spanTB","80%",1);
    createSpan(this,null,"30%","10%",null,spanB,"span spanTB","80%",1);
    createSpan(this,"10%",null,"10%",null,spanL,"span spanLR",1,"60%");
    createSpan(this,"10%",null,null,"10%",spanR,"span spanLR",1,"60%");
}

function createSpan(obj,top,bottom,left,right,span,className,width,height){
    span.style.top=top;
    span.style.bottom=bottom;
    span.style.left=left;
    span.style.right=right;
    span.className=className;
    setTimeout(function(){
        span.style.width=width;
        span.style.height=height;
    },10);
    obj.appendChild(span);
    span.style.transition="all 1s";
}
function dead(){
    var footerP=this.getElementsByClassName("footerP");
    for(var i=0;i<footerP.length;i++){
        footerP[i].style.visibility="hidden";
        footerP[i].style.top="-400px";
        footerP[footerP.length-1].style.top="-245px";
        footerP[footerP.length-1].style.visibility="visible";
        footerP[i].style.transition="visibility 0.1s,top 1s";
    }
    var span=this.getElementsByTagName("span");
    for(var i=span.length-1;i>=0;i--){
        this.removeChild(span[0]);
    }
}
//
//////////scroll事件
function scroll(event){
    var y=window.scrollY,
        thisTop=change1_2.style.top;
    //console.log(y)
    if(y<550){
        change1_2.style.top=50+"px";
    }else if(y>650&&thisTop>1+'px'){
        change1_2.style.top=1+'px';
        change1_2.background="url('../img/brand_bg.jpg')"
        change1_2.style.transition="all 0.5s";
    } else if(y<1333)
        this.getElementById("Experience").getElementsByTagName("img")[0].style.top=0+"px";
    else if(y>=1333&&y<1733){
        Experience.getElementsByTagName("img")[0].style.top=-y+1333+'px';
    }else if(y<1833){
        rightImg.getElementsByTagName("img")[0].style.left=-35+'px';
        leftImg.getElementsByTagName("img")[0].style.left=-100+'px';
    }
    else if(y>=1833){
        Experience.getElementsByTagName("img")[0].style.top=-400+"px";
        leftImg.getElementsByTagName("img")[0].style.left=-50+'px';
        leftImg.getElementsByTagName("img")[0].style.transition="all 0.5s";
        rightImg.getElementsByTagName("img")[0].style.left=-85+'px';
        rightImg.getElementsByTagName("img")[0].style.transition="all 0.5s";
    }
}
//////阻止事件冒泡
function stopPropagation(){
    this.stopPropagation;
}

function closeFlash(){
    oFlash1.style.display="none";
    document.body.style.overflow="inherit";
}

function openFlash(){
    oFlash1.style.display="block";
    document.body.style.overflow="hidden";
}